/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-08-17 15:44:15
 * @LastEditors: GGB
 * @LastEditTime: 2021-08-18 16:01:16
 */
import 'package:flutter/material.dart';

/// 虚线
class DottedLine extends StatelessWidget {
  final double height;
  final Color color;
  final Axis direction;
  final double width;
  final bool circle;
  final double margin;

  const DottedLine(
      {this.height = 1,
      this.color = Colors.black,
      this.direction = Axis.horizontal,
      this.width = 10,
      this.circle = false,
      this.margin = 1});

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        final boxWidth = direction == Axis.horizontal
            ? constraints.constrainWidth()
            : constraints.constrainHeight();

        final dashHeight = height;
        final dashWidth = width;
        final dashCount = (boxWidth / (2 * (dashWidth + margin))).floor();
        return Flex(
          children: List.generate(dashCount, (_) {
            return Container(
              margin: EdgeInsets.all(margin),
              width: direction == Axis.horizontal ? dashWidth : dashHeight,
              height: direction == Axis.horizontal ? dashHeight : dashWidth,
              decoration: BoxDecoration(
                color: color,
                shape: circle ? BoxShape.circle : BoxShape.rectangle,
              ),
            );
          }),
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          direction: direction,
        );
      },
    );
  }
}
